/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Modele.Contact;
import Modele.FileManipulation;
import Vue.Miniature;
import Vue.ViewContactPanel;
import Vue.Window;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

/**
 *The accurate view listener is need in order to see in detail the contact the user click on
 * So we need the Jtable, the list of contact, and the view contact panel
 * @author Bakalian
 */
public class AccurateViewListener implements MouseListener{

    private JTable myTable=null;
    private List<Contact> myContacts;
    private ViewContactPanel displayPanel;
    private int SelectedRow;
    private final Window myWindow;
   
  
     /**
      * 
      * @param t
      * @param w
      * @param display 
      */
    public AccurateViewListener (JTable t,Window w,ViewContactPanel display){
        myTable=t;
        displayPanel=display;
        myWindow=w;
        myContacts=w.getMyListofContact();
        
      
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
       
        Point p = e.getPoint();
       myWindow.getMyContactPanel().setIdNumber(Integer.parseInt(myWindow.getMyListofContact().get(myWindow.getMyContactPanel().getMyTable().getRowSorter().convertRowIndexToModel(myWindow.getMyContactPanel().getMyTable().rowAtPoint(e.getPoint()))).getIdNumber()));
       System.out.println("test id number AVL :" + myWindow.getMyContactPanel().getIdNumber());
       int idnumber=myWindow.getMyContactPanel().getIdNumber();
       
        
        // get the row index that contains that coordinate
	
 
        /**
         * What we do is to change the content of the accurate view of the contact
         * we need to convert the index given by the jtable thanks to it rowSorterconverter
         * because avec Sorting contact the index has changed, an we need the original one
         */
        if (e.getClickCount() == 1){
           
            
          
          for(int i=0;i<myWindow.getMyListofContact().size();i++){
              
              if(Integer.parseInt(myWindow.getMyListofContact().get(i).getIdNumber())==idnumber){
                  
                    displayPanel.getFirstName().setText(myWindow.getMyListofContact().get(i).getFirstName());
                    displayPanel.getLastName().setText(myWindow.getMyListofContact().get(i).getLastName());
                    displayPanel.getPhone().setText(myWindow.getMyListofContact().get(i).getPersonalPhone());
                    displayPanel.getEmail().setText(myWindow.getMyListofContact().get(i).getEmail());
                    displayPanel.getHomepage().setText(myWindow.getMyListofContact().get(i).getHomePage());
                    displayPanel.getAddress().setText(myWindow.getMyListofContact().get(i).getHomeAddress());
                    
                    
                    if(myWindow.getMyListofContact().get(i).getPhoto().isEmpty()==false){
               
              ImageIcon image=new ImageIcon(new ImageIcon(myWindow.getMyListofContact().get(i).getPhoto()).getImage().getScaledInstance(150,150, Image.SCALE_DEFAULT));
              displayPanel.getM().getLabel().setIcon(image);
              
           }
          else {
               ImageIcon image=new ImageIcon(new ImageIcon("photo.jpg").getImage().getScaledInstance(150,150, Image.SCALE_DEFAULT));
               displayPanel.getM().getLabel().setIcon(image);
               
           }
                    
                    
                    
              }
           
        }
           SelectedRow=myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()));
// recorde the selected Row
        /*  
           displayPanel.getFirstName().setText(mynewlist.get(SelectedRow).getFirstName());
           displayPanel.getLastName().setText(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getLastName());
           displayPanel.getPhone().setText(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getPersonalPhone());
           displayPanel.getAddress().setText(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getHomeAddress());
           displayPanel.getEmail().setText(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getEmail());
          displayPanel.getHomepage().setText(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getHomePage());
          int idNumer=Integer.parseInt(mynewlist.get(SelectedRow).getIdNumber());//picture set defaut to photo.jpg if no photo selected 
          System.out.println("test  ----------> id Number" +idNumer);
          if(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getPhoto().isEmpty()==false){
               
              ImageIcon image=new ImageIcon(new ImageIcon(mynewlist.get(myTable.getRowSorter().convertRowIndexToModel(myTable.rowAtPoint(e.getPoint()))).getPhoto()).getImage().getScaledInstance(150,150, Image.SCALE_DEFAULT));
              displayPanel.getM().getLabel().setIcon(image);
              
           }
          else {
               ImageIcon image=new ImageIcon(new ImageIcon("photo.jpg").getImage().getScaledInstance(150,150, Image.SCALE_DEFAULT));
               displayPanel.getM().getLabel().setIcon(image);
               
           }
          */if(SwingUtilities.isRightMouseButton( e )){
              
             /**
              * if we right click on the jtable we show the popupMenu
              */
              JPopupMenu popupMenu = new JPopupMenu();
              JMenuItem menuItem = new JMenuItem("add to a group" );
              popupMenu.add(menuItem);
              myTable.add(popupMenu);
              
              
              
          
          }
          
         
          
            
        
        }
        if (SwingUtilities.isRightMouseButton( e )) {
           JPopupMenu jpopupmenu = new JPopupMenu();
          JMenuItem jmenuitem1 = new JMenuItem("AddToGroup" );
          jpopupmenu.add(jmenuitem1);
           
        }
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mousePressed(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public int getSelectedRow() {
        return SelectedRow;
    }

    public List<Contact> getMyContacts() {
        return myContacts;
    }
    
}
